/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "service_stub.h"
#include "common_utils.h"
#include "isecure_element_reader.h"
#include "ipc_skeleton.h"
#include "loghelper.h"
#include "message_parcel.h"
#include "se_common_exception.h"
#include "se_service_ipc_interface_code.h"

using namespace std;
namespace OHOS {
namespace SE {
namespace OMAPI {
int ServiceStub::OnRemoteRequest(uint32_t code,         /* [in] */
                                 MessageParcel& data,   /* [in] */
                                 MessageParcel& reply,  /* [out] */
                                 MessageOption& option) /* [in] */
{
    InfoLog("ServiceStub::OnRemoteRequest occur, code is %{public}d, CallingPid = %{public}d",
        code, IPCSkeleton::GetCallingPid());
    if (data.ReadInterfaceToken() != GetDescriptor()) {
        ErrorLog("ServiceStub OnRemoteRequest GetDescriptor failed");
        return ErrorCode::SE_IPC_TOKEN_ERROR;
    }
    switch (code) {
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_SERVICE_GET_READERS):
            return HandleGetReaders(data, reply);
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_SERVICE_GET_READER_IFACE):
            return HandleGetReaderInterface(data, reply);
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_SERVICE_IS_NFC_EVENT_ALLOWED):
            return HandleIsNfcEventAllowed(data, reply);
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_SERVICE_SHUTDOWN):
            return HandleShutdown(data, reply);
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_SERVICE_REGISTER_CALLER_LISTENER):
            return HandleRegisterCallerListener(data, reply);
        default:
            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
    }
}
int ServiceStub::HandleGetReaders(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    std::vector<std::string> readerList;
    int statusCode = GetReaders(readerList);
    reply.WriteInt32(statusCode);
    reply.WriteStringVector(readerList);
    return ERR_NONE;
}

int ServiceStub::HandleGetReaderInterface(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    std::string readerName = data.ReadString();
    sptr<OMAPI::ISecureElementReader> reader;
    int statusCode = GetReaderInterface(readerName, reader);
    if (reader) {
        reply.WriteRemoteObject(reader->AsObject());
    } else {
        statusCode = ErrorCode::SE_ILLEGAL_PARAMETER_ERROR;
    }
    reply.WriteInt32(statusCode);
    return ERR_NONE;
}

int ServiceStub::HandleIsNfcEventAllowed(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    // unhandle currently, just return success.
    reply.WriteInt32(ErrorCode::SE_SUCCESS);
    reply.WriteBool(false);
    return ERR_NONE;
}

int ServiceStub::HandleShutdown(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    int statusCode = ErrorCode::SE_SUCCESS;
    statusCode = Shutdown();
    reply.WriteInt32(statusCode);
    return ERR_NONE;
}

int ServiceStub::HandleRegisterCallerListener(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
    int statusCode = ErrorCode::SE_SUCCESS;
    if (remoteObject) {
        statusCode = RegisterCallerListener(remoteObject);
    } else {
        statusCode = ErrorCode::SE_ILLEGAL_PARAMETER_ERROR;
    }
    reply.WriteInt32(statusCode);
    return ERR_NONE;
}
}  // namespace OMAPI
}  // namespace SE
}  // namespace OHOS